package LeetCode.LeetCode2;



public class Num18 {
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
    public ListNode deleteNode(ListNode head, int val) {
        if(head == null){
            return null;
        }
        if(head.val == val){
            head = head.next;
        }
        ListNode node = head.next;
        ListNode prev = head;
        while (node != null){
            if(node.val == val){
                prev.next = node.next;
                node.next = null;
            }
            node = node.next;
            prev = prev.next;
        }
        return head;
    }
}
